#include "4p.h"

#ifdef WIN32
cell lasterr;
cell winerr(cell t)
{
  lasterr = t ? 0 : GetLastError();
  return t;
}
#define MSGMAX 1024
char errmsg[MSGMAX];
char *winerrmsg(void)
{
  LPVOID lpMsgBuf;
  if (!lasterr)
    return NULL;
  if (!FormatMessage( 
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM | 
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL
  ))
    strcpy(errmsg, "unknown error");
  else {
    errmsg[MSGMAX - 1] = '\0';
    strncpy(errmsg, (LPCTSTR)lpMsgBuf, MSGMAX - 1);
    LocalFree(lpMsgBuf);
  }
  lasterr = 0;
  return errmsg;
}
#endif /* WIN32 */
